package com.quanquan.user.service.statisticsReport;

import com.quanquan.user.entity.IntegralTradingDetails;
import com.quanquan.user.repository.IntegralDetailsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述: 积分报表逻辑层
 *
 * @ClassName: IntegralStatisticsReportService
 * @Author: xiaoqianh丶
 * @Date: 2019-05-29 13:40
 * @Version: V1.0
 */
@Service
public class IntegralStatisticsReportService {

    @Autowired
    private IntegralDetailsRepository integralDetailsRepository;
    /**
     * 按时间查询（报表使用）（获得）
     * @param startTime
     * @param endTime
     * @return
     */
    public List<IntegralTradingDetails> statisticalReportsReceive(Date startTime, Date endTime) {
        return integralDetailsRepository.findAll((Specification<IntegralTradingDetails>) (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            predicates.add(cb.greaterThanOrEqualTo(root.get("createTime").as(Date.class),startTime));
            predicates.add(cb.lessThanOrEqualTo(root.get("createTime").as(Date.class), endTime));
            predicates.add(cb.equal(root.get("tradeAction"), 0));
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        });
    }



    /**
     * 按时间查询（报表使用）（消费）
     * @param startTime
     * @param endTime
     * @return
     */
    public List<IntegralTradingDetails> statisticalReportsConsume(Date startTime, Date endTime) {
        return integralDetailsRepository.findAll((Specification<IntegralTradingDetails>) (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            predicates.add(cb.greaterThanOrEqualTo(root.get("createTime").as(Date.class), startTime));
            predicates.add(cb.lessThanOrEqualTo(root.get("createTime").as(Date.class), endTime));
            predicates.add(cb.equal(root.get("tradeAction"), 1));
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        });
    }


    /**
     * 本年金额获得总额
     * @param startTime
     * @param endTime
     */
    public Integer statisticalReportsReceiveYear(Date startTime, Date endTime) {
        return integralDetailsRepository.findIntegralByNum(startTime, endTime);
    }
    /**
     * 本年金额扣除总额
     * @param startTime
     * @param endTime
     */
    public Integer statisticalReportsConsumeYear(Date startTime, Date endTime) {
        return integralDetailsRepository.findIntegralConsumeByNum(startTime, endTime);
    }
}
